//
//  TimelineViewController.m
//  Aun
//
//  Created by Alexander on 8/24/10.
//  Copyright 2010 KTH Royal Institute of Technology. All rights reserved.
//

#import "TimelineViewController.h"
#import "TimelineCell.h"
#import "PortalViewController.h"
#import "DAO.h"
#import "Patient.h"

@implementation TimelineViewController

@synthesize listData;
@synthesize childViewController;

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
    NSMutableArray *array = [[DAO getInstance] getPatients];
	[self setListData:array];
}

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 150;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[self listData] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	static NSString *cellIdentifier = @"TimelineCellIdentifier";
	TimelineCell *cell = (TimelineCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
	if (cell == nil) {
		NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TimelineCell" owner:self options:nil];
		cell = [nib objectAtIndex:0];
	}
	[[cell infoLabel] setText:[[listData objectAtIndex:[indexPath row]] name]];
	[cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
	return cell;
}

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
	Patient *p = [listData objectAtIndex:[indexPath row]];
    if (childViewController == nil)
		childViewController = [[PortalViewController alloc] initWithNibName:@"Portal" bundle:nil];
	NSString *g = [[NSString alloc] initWithFormat:@"%@ - %@", [p name], [p identifier]];
	[childViewController setTitle:g];
	[g release];
	[childViewController setPatient:p];
	UINavigationController *nav = (UINavigationController *)[self parentViewController];
	[nav pushViewController:childViewController animated:YES];
}

@end
